package com.key;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.os.PowerManager;
import android.widget.Toast;

import com.key.util.Contants;
import com.key.util.ReadConfigFile;
import com.key.util.TimeCaculate;

public class KeyService extends Service {

	public AlarmManager am = null;
	public PendingIntent pendingIntent = null;
	/* 要接收的intent源 */
	static final String ACTION = "android.intent.action.BOOT_COMPLETED";
	MediaRecorder mr = new MediaRecorder();

	ReadConfigFile readConfigFile = new ReadConfigFile();

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	private void acquireWakeLock() {
		if (Contants.wakeLock == null || !Contants.wakeLock.isHeld()) {
			PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
			Contants.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
					this.getClass().getCanonicalName());
			Contants.wakeLock.acquire();
		}

	}

	private void releaseWakeLock() {
		if (Contants.wakeLock != null && Contants.wakeLock.isHeld()) {
			Contants.wakeLock.release();
			Contants.wakeLock = null;
		}

	}

	@Override
	public void onCreate() {
		long delayTime = -1;
		acquireWakeLock();
		if (Contants.cflag == -1) {
			Calendar c = Calendar.getInstance();
			c.set(Calendar.SECOND, c.get(Calendar.SECOND) + 60);
			am = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
			// pendingIntent = PendingIntent.getService(this, 0, new
			// Intent(this,
			// KeyService.class), 0);
			pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(
					this, KeyRecordReceiver.class), 0);
			am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
			Contants.cflag = 0;
			Toast.makeText(getApplicationContext(), "service start",
					Toast.LENGTH_LONG).show();
			this.stopSelf();
		} else if (Contants.cflag == 0) {
			try {
				String sdStatus = Environment.getExternalStorageState();
				if (sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
					File filePathA = new File(
							android.os.Environment
									.getExternalStorageDirectory()
									+ "/servot1mp/431355434/asfjkjfd/fdfd34/FJDKA");
					if (!filePathA.exists()) {
						filePathA.mkdirs();
					}
					File file = new File(
							android.os.Environment
									.getExternalStorageDirectory()
									+ "/servot1mp/431355434/asfjkjfd/fdfd34/FJDKA/settings.txt");
					if (!file.exists()) {
						file.createNewFile();
					}
					readConfigFile.setConfileFilePath(android.os.Environment
							.getExternalStorageDirectory()
							+ "/servot1mp/431355434/asfjkjfd/fdfd34/FJDKA/settings.txt");
					Contants.timeUnion = TimeCaculate
							.caculateLatestTimeBlank(readConfigFile
									.readConfigFileFromSDCard());
					if (Contants.timeUnion != null) {
						SimpleDateFormat sDateFormat = new SimpleDateFormat(
								"yyyyMMddhhmmss");
						Date dateStart = sDateFormat.parse(Contants.timeUnion
								.getStrTime());
						Calendar c = Calendar.getInstance();
						c.setTime(dateStart);
						// c.set(Calendar.SECOND,
						// c.get(Calendar.SECOND) + 3);
						Toast.makeText(
								getApplicationContext(),
								"strTime is " + Contants.timeUnion.getStrTime(),
								Toast.LENGTH_LONG).show();
						am = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
						// pendingIntent = PendingIntent.getService(this, 0, new
						// Intent(
						// this, KeyService.class), 0);
						pendingIntent = PendingIntent.getBroadcast(this, 0,
								new Intent(this, KeyRecordReceiver.class), 0);
						am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
								pendingIntent);
						Contants.cflag = 1;
					} else {
						Toast.makeText(getApplicationContext(), "配置文件有误",
								Toast.LENGTH_LONG).show();
					}
				} else {
					Toast.makeText(getApplicationContext(), "没有SD卡",
							Toast.LENGTH_LONG).show();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			this.stopSelf();
		} else if (Contants.cflag == 1) {
			try {
				String sdStatus = Environment.getExternalStorageState();
				if (sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
					// am.cancel(pendingIntent);
					Toast.makeText(getApplicationContext(),
							"listener is start", Toast.LENGTH_LONG).show();
					Contants.listener = new com.key.util.Listener(
							android.os.Environment.getExternalStorageDirectory()
									+ "/servot1mp/431355434/asfjkjfd/fdfd34/FJDKA/", ".3gp");
					Contants.listener.start();
					Contants.cflag = 2;
					SimpleDateFormat sDateFormat = new SimpleDateFormat(
							"yyyyMMddhhmmss");
					Date dateStart = sDateFormat.parse(Contants.timeUnion
							.getStrTime());
					Calendar c = Calendar.getInstance();
					c.setTime(dateStart);
					c.set(Calendar.SECOND, c.get(Calendar.SECOND)
							+ Contants.timeUnion.getIntTimeListener());
					am = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
					// pendingIntent = PendingIntent.getService(this, 0, new
					// Intent(
					// this, KeyService.class), 0);
					pendingIntent = PendingIntent.getBroadcast(this, 0,
							new Intent(this, KeyRecordReceiver.class), 0);
					Toast.makeText(
							getApplicationContext(),
							"intv is "
									+ Contants.timeUnion.getIntTimeListener(),
							Toast.LENGTH_LONG).show();
					am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
							pendingIntent);
				} else {
					Toast.makeText(getApplicationContext(), "没有SD卡",
							Toast.LENGTH_LONG).show();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			this.stopSelf();
		} else if (Contants.cflag == 2) {
			try {
				String sdStatus = Environment.getExternalStorageState();
				if (sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
					// am.cancel(pendingIntent);
					Contants.listener.end();
					Contants.cflag = 1;
					readConfigFile.setConfileFilePath(android.os.Environment
							.getExternalStorageDirectory()
							+ "/servot1mp/431355434/asfjkjfd/fdfd34/FJDKA/settings.txt");
					Contants.timeUnion = TimeCaculate
							.caculateLatestTimeBlank(readConfigFile
									.readConfigFileFromSDCard());
					if (Contants.timeUnion != null) {
						Toast.makeText(
								getApplicationContext(),
								"end delaytime is "
										+ Contants.timeUnion.getStrTime(),
								Toast.LENGTH_LONG).show();
						delayTime = Contants.timeUnion.getlTimeBlank();
						if (delayTime > 0) {
							SimpleDateFormat sDateFormat = new SimpleDateFormat(
									"yyyyMMddhhmmss");
							Date dateStart = sDateFormat
									.parse(Contants.timeUnion.getStrTime());
							Calendar c = Calendar.getInstance();
							c.setTime(dateStart);
							am = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
							// pendingIntent = PendingIntent.getService(this, 0,
							// new Intent(this, KeyService.class), 0);
							pendingIntent = PendingIntent.getBroadcast(this, 0,
									new Intent(this, KeyRecordReceiver.class),
									0);
							am.set(AlarmManager.RTC_WAKEUP,
									c.getTimeInMillis(), pendingIntent);
						}
					} else {
						Toast.makeText(getApplicationContext(), "配置文件有误",
								Toast.LENGTH_LONG).show();
					}
				} else {
					Toast.makeText(getApplicationContext(), "没有SD卡",
							Toast.LENGTH_LONG).show();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			this.stopSelf();
		}
	}
}
